using System;
using System.Collections.Generic;
using System.Text;
using Wrath.Xml;

namespace Wrath.CodeBox
{
    public class Token
    {
        public string Type;
        public string Value;

        public override string ToString()
        {
            switch (Type)
            {
                case "whitespace": return Value;
                default :
                    return "{" + Type + ": " + Value + "}";
            }
        }

        public string Xml
        {
            get
            {
                // preserves whitespace in comments or strings by using extended encode
                return "<token type='" + Element.XmlEncode(Type, false) + "'>" + Element.XmlEncode(Value, true) + "</token>";
            }
            set
            {
                try
                {
                    FileParser parser = new FileParser(value);
                    Element token = parser.FindFirstElement("token");
                    Type = token.FindAttribute("type").Value;

                    // restores whitespace in comments or strings by using decode
                    Value = Element.XmlDecode(token.Value);
                }
                catch (Exception ex)
                {
                    Type = "";
                    Value = null;
                    throw ex;
                }
            }
        }

        public Token(string type, string value)
        {
            Type = type;
            Value = value;
        }
  
    }
}
